package com.rehome.udp;


import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ReminderTextAdapter extends FocusAdapter {
    //JTextField提示文字通用方法
    JTextField textField;
    String reminderString;

    //初始化
    public ReminderTextAdapter(JTextField textField, String reminderString) {
        this.textField = textField;
        this.reminderString = reminderString;
    }

    //焦点获得
    @Override
    public void focusGained(FocusEvent e) {
        String tempString = textField.getText();
        //String tempString = this.getText();
        if (tempString.equals(reminderString)) {
            textField.setText("");
            textField.setForeground(Color.BLACK);
        }
    }

    //焦点失去
    @Override
    public void focusLost(FocusEvent e) {
        String tempString = textField.getText();
        if (tempString.equals("")) {
            textField.setForeground(Color.GRAY);
            textField.setText(reminderString);
        }
    }

}
